package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Log
    @PostMapping
    public Result add(@RequestBody Clue clue) {
        log.info("添加线索");
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clue.setStatus(1);
        clueService.save(clue);
        return Result.success();
    }

    @GetMapping
    public Result list(ClueDto clueDto) {
        log.info("查询线索");
        PageResult<Clue> pageResult = clueService.listclues(clueDto);
        return Result.success(pageResult);
    }
    @Log
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("分配线索: {},{}", clueId, userId);
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setStatus(2);
        clue.setUserId(userId);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }
    @GetMapping("{id}")
    public Result getbyid(@PathVariable Integer id)
    {
        log.info("根据ID查询线索: {}" , id);
        Clue clue = clueService.cluebyid(id);
        return Result.success(clue);
    }
    @Log
    @PutMapping
    public Result trackCLue (@RequestBody Clue clue)
    {
        log.info("线索跟踪: {}" , clue);
        clueService.updateTrackClue(clue);
        return Result.success();
    }
    @Log
    @PutMapping("/false/{id}")
    public Result falseClue(@PathVariable Integer id, @RequestBody MarkFalseClueDto markFalseClueDto)
    {
        log.info("线索作废: {}",id);
        clueService.falseClue(id, markFalseClueDto);
        return Result.success();
    }
    @GetMapping("/pool")
    public Result listPool(ClueDto clueDto)
    {
        log.info("查询线索");
        PageResult<Clue> pageResult = clueService.listcluesPool(clueDto);
        return Result.success(pageResult);
    }
    @Log
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id)
    {
        log.info("线索转商机: {}",id);
        clueService.toBusiness(id);
        return Result.success();
    }
}
